package com.hdp.jetpack_mvvm.livedata

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.HasDefaultViewModelProviderFactory
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hdp.jetpack_mvvm.R
import kotlinx.android.synthetic.main.activity_view_model1.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LiveDataActivity1 : AppCompatActivity() {
    val TAG = "ViewModelActivity1"
    private lateinit var mViewModel1: ViewModel1
    private lateinit var mViewModel2: ViewModel2
    private lateinit var mViewModel3: ViewModel3

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_model1)
        init()
    }

    fun init() {
        //全局的LiveData
        SingletonLiveData.getName().observe(this, {
            edit_name_global.setText(it)
        })
        //1、不传入生命周期
        mViewModel1 = ViewModelProvider.NewInstanceFactory().create(ViewModel1::class.java)

        //2、使用默认Factory
        mViewModel1 = ViewModelProvider(this).get(ViewModel1::class.java)

//        3、传入Factory创建viewmodel
//        mViewModel1 = ViewModelProvider(
//            this,
//            ViewModelProvider.NewInstanceFactory()
//        ).get(ViewModel1::class.java)

        //4、使用AndroidViewModelFactory获取，通过这种方式可以在ViewModel中获取到Application
//        mViewModel2 =
//            ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
//                ViewModel2::class.java
//            )

        Log.e(TAG, mViewModel1.toString())
        //用户名
        mViewModel1.getName().observe(this, { name ->
            edit_name.setText(name)
        })
        //密码
        mViewModel1.getPwd().observe(this, { pwd ->
            edit_pwd.setText(pwd)
        })
        //提交按钮
        mViewModel1.getCanSubmit().observe(this, { enable ->
            btn_submit.isEnabled = enable
        })
        //5、能保存状态的ViewModel
        mViewModel3 = ViewModelProvider(
            this,
            SavedStateViewModelFactory(application, this)
        ).get(ViewModel3::class.java)
        mViewModel3.getName().observe(this, {
            Toast.makeText(this, it, Toast.LENGTH_LONG).show()
        })
        mockLoadData()
        btn_submit.setOnClickListener {
            startActivity(Intent(this, LiveDataActivity2::class.java))
        }
        btn_set_state.setOnClickListener {
            val str = edit_save_state.text.toString()
            mViewModel3.saveNewName(str)
        }
    }

    private fun mockLoadData() {
        GlobalScope.launch(Dispatchers.Main) {
            delay(1000)
            mViewModel1.loadName()
            delay(1000)
            mViewModel1.loadPwd()
        }
    }
}